package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.ehp.bp.bo.payment.NotifyBusinessRefundBO;
import com.naiterui.ehp.bp.constants.OrderSnType;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.utils.payment.bo.NotifyBusinessPayBO;
import com.naiterui.ehp.bs.patient.service.IConsultSessionService;
import com.naiterui.ehp.bs.patient.service.IVideoConsultService;
import com.naiterui.ehp.bs.patient.utils.exception.PatientBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <pre>
 *     支付服务回调通知
 * </pre>
 *
 * @author 王泽浩
 * @since 1.0.0
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.NOTIFY})
@Controller
@ResponseBody
@RequestMapping("ap/inner/notify")
public class ApInnerNotifyPayController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApInnerNotifyPayController.class);

    @Autowired
    private IConsultSessionService consultSessionService;
    @Autowired
    private IVideoConsultService videoConsultService;

    @PostMapping(value = "pay")
    public String notifyPay(@RequestBody NotifyBusinessPayBO request) throws Exception {
        LOGGER.info("支付回调通知数据：{}", request);
        String orderSn = request.getOrderSn();
        Integer orderType = request.getOrderType();
        Integer status = request.getOrderStatus();
        Integer totalFee = request.getTotalFee();
        switch (orderType) {
            /*// 个性化收费
            case OrderSnType.CUSTOM_CHARGE_ORDERSN:
                LOGGER.info("订单号类型为[个性化服务]，开始处理业务逻辑。订单号:{}", orderSn);
                this.drCustomChargeOrderService.updateNotifyPay(orderSn, status);
                break;*/
            // 图文收费
            case OrderSnType.MESSAGE_CONSULT_ORDERSN:
                LOGGER.info("订单号类型为[图文咨询收费]，开始处理业务逻辑。订单号:{}", orderSn);
                this.consultSessionService.updateNotifyPay(orderSn, status);
                break;
            // 视频
            case OrderSnType.VIDEO_ORDERSN:
                LOGGER.info("订单号类型为[视频]，开始处理业务逻辑。订单号:{}", orderSn);
                this.videoConsultService.payNotify(orderSn, status);
//                this.patientReservationService.updatePayStatus(orderSn,
//                        "1", request.getChannelTradeSn(), totalFee + "");
                break;
            case OrderSnType.SELF_INSPECT_TEST:
                LOGGER.info("订单号类型为[自检测试]，订单号:{}, 订单状态:{}", orderSn, status);
                break;
            /*// 打赏
            case OrderSnType.APPRAISE_ORDERSN:
                LOGGER.info("订单号类型为[打赏]，开始处理业务逻辑。订单号:{}", orderSn);
                this.payInfoService.updateNotifyPay(request);
                break;
            //咨询活动
            case OrderSnType.CONSULT_ACTIVITY_ORDER:
                LOGGER.info("订单号类型为[咨询活动]，开始处理业务逻辑。订单号:{}", orderSn);
                this.consultActivityService.updateOrderNotifyStatus(request);
                break;*/
            default:
                LOGGER.info("订单号类型不匹配。订单号:{}", orderSn);
                break;
        }
        return "success";
    }

    @PostMapping("refund")
    @ApiOperation(value = "退款成功通知")
    public String notifyRefund(@RequestBody NotifyBusinessRefundBO request) throws PatientBusinessException {
        LOGGER.info("退款回调通知数据：{}", request);
        String orderSn = request.getOrderSn();
        Integer orderType = request.getOrderType();
        switch (orderType) {
            // 图文收费
            case OrderSnType.MESSAGE_CONSULT_ORDERSN:
                LOGGER.info("退款订单号类型为[图文咨询收费]，开始处理业务逻辑。订单号:{}", orderSn);
                this.consultSessionService.updateNoticePatient(request.getRefundStatus() == NotifyBusinessRefundBO.REFUND_SUCCESS, orderSn);
                break;
            case OrderSnType.VIDEO_ORDERSN:
                LOGGER.info("退款订单号类型为[视频咨询收费]，开始处理业务逻辑。订单号:{}", orderSn);
                this.videoConsultService.refundNotify(orderSn, request.getRefundStatus());
                break;
            case OrderSnType.SELF_INSPECT_TEST:
                LOGGER.info("退款订单号类型为[自检测试]，退款成功");
                break;
            /*case OrderSnType.CONSULT_ACTIVITY_ORDER:
                LOGGER.info("退款订单号类型为[咨询活动]，开始处理业务逻辑。订单号:{}", orderSn);
                this.consultActivityService.updateOrderNotifyRefundStatus(request);
                break;*/
            default:
                LOGGER.warn("对应订单号没有提供线上退款业务。订单号:{}", orderSn);
                break;
        }
        return "success";
    }
}
